package com.iampzh.springvue.controller;



import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iampzh.springvue.comm.MyResult;
import com.iampzh.springvue.entity.LoginUser;
import com.iampzh.springvue.entity.User;
import com.iampzh.springvue.listener.ExcelReadListener;
import com.iampzh.springvue.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;


    @GetMapping("/users")
    public List<User> findAllUser(){
        return userMapper.selectList(null);
    }
    @PostMapping("/save")
    public Integer save(@RequestBody User user){
        if(user.getId()==null){
            return userMapper.insert(user);
        }else{
            return userMapper.updateById(user);
        }

    }
    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id){
        return userMapper.deleteById(id);
    }
    @GetMapping("/page")
    public Page<User> findUsersPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String username,@RequestParam(defaultValue = "") String email,@RequestParam(defaultValue = "") String address
    ){
        Page<User> page=new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(!"".equals(username)){
            queryWrapper.like("username",username);
        }
        if(!"".equals(email)){
            queryWrapper.like("email",email);
        }
        if(!"".equals(address)){
            queryWrapper.like("address",address);
        }
        queryWrapper.orderByDesc("id");
        return userMapper.selectPage(page,queryWrapper);
    }
    @PostMapping("deleteIds")
    public Integer deleteIds(@RequestBody List<Integer> ids){


        return userMapper.deleteBatchIds(ids);
    }
    @GetMapping("/export")
    public void writeExcel(HttpServletResponse response) throws IOException {
        List<User> users = userMapper.selectList(null);

        EasyExcel.write("d:/用户信息.xlsx",User.class).sheet("用户信息表").doWrite(users);

    }
    @PostMapping("/readExcel")
    public void readExcel(@RequestParam MultipartFile file) throws IOException {
       EasyExcel.read(file.getInputStream(),User.class,new ExcelReadListener(userMapper)).sheet().doRead();

    }
    @PostMapping("/login")
    public MyResult login(@RequestBody LoginUser loginuser){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",loginuser.getUsername());
        queryWrapper.eq("password",loginuser.getPassword());
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){


            LoginUser loginUser=new LoginUser();
            loginUser.setUsername(user.getUsername());
            loginUser.setAvaurl(user.getAvaurl());
//            loginUser.setToken(token);


            return MyResult.success(loginUser);
        }else{
            return MyResult.error401();
        }
    }

    @PostMapping("/register")
    public MyResult register(@RequestBody LoginUser user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        List<User> users = userMapper.selectList(queryWrapper);

        if(users.size()>0){
            return new MyResult("405","用户名已存在",null);
        }else{
            User user2 = new User();
            user2.setUsername(user.getUsername());
            user2.setPassword(user.getPassword());
            userMapper.insert(user2);
            return MyResult.success();
        }
    }
}
